from django.db.models import *

from extensions.models import *


from apps.common.models import GzdeBaseModel

#附件分组
class AttachmentGroup(GzdeBaseModel):
    """附件分组"""
    parent = ForeignKey('self', on_delete=SET_NULL, null=True, blank=True, related_name='children', verbose_name='上级分组',db_constraint=False)
    sort = IntegerField(unique=False, default=None, blank=True, verbose_name='排序方式')  # 数字越小越靠前

    class Meta:
        unique_together = (('name','parent',),)

#附件支持图片、视频、音频、文档、压缩包
class Attachment(GzdeBaseModel):
    """附件"""
    #附件分组
    name = CharField(max_length=64, verbose_name='文件名')
    group = ForeignKey(AttachmentGroup, on_delete=SET_NULL, null=True, blank=True, verbose_name='附件分组',db_constraint=False)
    #附件名称
    file = FileField(upload_to='attachment', verbose_name='附件')
    #附件来源，1为系统，2为用户上传
    source = IntegerField(unique=False, default=1, blank=True, verbose_name='附件来源')
    #附件类型，1为图片，2为视频，3为音频，4为文档，5为压缩包
    type = IntegerField(unique=False, default=1, blank=True, verbose_name='附件类型')
    #附件大小
    size = IntegerField(unique=False, default=0, blank=True, verbose_name='附件大小')
    #附件封面
    cover = CharField(max_length=255, unique=False, blank=True, verbose_name='附件封面')
    #附件扩展名
    extension = CharField(max_length=255, unique=False, blank=True, verbose_name='附件扩展名')
    #是否为小程序首页轮播图
    is_index = IntegerField(unique=False, default=0, blank=True, verbose_name='是否为首页轮播图')

    class Meta:
        unique_together = (('name', 'file',),)


#商品分类
class GoodsCategory(GzdeBaseModel):
    """商品分类"""
    name = CharField(max_length=64, verbose_name='名称')
    parent = ForeignKey('self', on_delete=SET_NULL, null=True, blank=True, related_name='children', verbose_name='上级分类', db_constraint=False)
    #分类图片
    image = ForeignKey(Attachment, on_delete=SET_NULL, null=True, blank=True, verbose_name='分类图片', db_constraint=False)
    #状态1显示0不显示
    status = IntegerField(unique=False, default=1, blank=True, verbose_name='状态')
    sort = IntegerField(unique=False, default=None, blank=True, verbose_name='排序方式')#数字越小越靠前

    class Meta:
        unique_together = (('name', 'parent',),)


#轮播图
class IndexBanner(GzdeBaseModel):
    """轮播图"""
    # 订单状态选项
    TYPE_CHOICES = [
        ("shouye", "首页"),
        ("zhongxiao", "中小市场"),
        ("xinxihua", "信息化集成"),
        ("gongcheng", "工程施工"),
        ("weihu", "网络维护"),
        ("zhineng", "智能家居"),
        ("jiliang", "计量检测"),
        ("cailiao", "材料设备"),
        ("shigong", "施工队伍"),
        ("zizhi", "企业资质"),
        ("anli", "案例分享"),
    ]
    #轮播图名称
    name = CharField(max_length=64, verbose_name='轮播图名称')
    #轮播图图片
    image = ForeignKey(Attachment, on_delete=SET_NULL, null=True, blank=True, verbose_name='轮播图图片', db_constraint=False)
    #轮播图链接
    link = CharField(max_length=255, unique=False,null=True, blank=True, verbose_name='轮播图链接')
    #轮播图排序
    sort = IntegerField(unique=False, default=None, blank=True, verbose_name='轮播图排序')#数字越小越靠前
    #轮播图商品分类
    category = ForeignKey(GoodsCategory, on_delete=SET_NULL, null=True, blank=True, verbose_name='轮播图分类', db_constraint=False)
    #轮播图类别
    type = CharField(max_length=64,unique=False, default='shouye', choices=TYPE_CHOICES,blank=True, verbose_name='轮播图类别')#shouye为首页，2为商品，3为链接


    class Meta:
        unique_together = (('name', 'image',),)




#商品服务
class GoodsService(GzdeBaseModel):
    """商品服务"""
    #服务概述
    summary = CharField(max_length=255, unique=False, blank=True, verbose_name='服务概述')
    #是否默认选中
    is_default = IntegerField(unique=False, default=0, blank=True, verbose_name='是否默认选中')
    sort = IntegerField(unique=False, default=None, blank=True, verbose_name='排序方式')#数字越小越靠前
    class Meta:
        unique_together = (('name',),)


#商品
class Goods(GzdeBaseModel):
    #商品编号
    good_no = CharField(max_length=64, unique=False,null=True, default=1, blank=True, verbose_name='商品编号')
    #商品分类
    category = ForeignKey(GoodsCategory, on_delete=SET_NULL, null=True, blank=True, verbose_name='商品分类', db_constraint=False)
    #商品规格
    specification = CharField(max_length=255, unique=False, null=True, blank=True, verbose_name='商品规格')
    #商品名称
    name = CharField(max_length=64, verbose_name='商品名称')
    #商品图片，多对多
    images = ManyToManyField(Attachment, null=True, blank=True, verbose_name='商品图片', related_name='Goods_images', db_constraint=False)
    #主图视频
    video = ForeignKey(Attachment, on_delete=SET_NULL, null=True, blank=True, verbose_name='主图视频', related_name='Goods_video', db_constraint=False)
    #主图视频封面,外键
    video_cover = ForeignKey(Attachment, on_delete=SET_NULL, null=True, blank=True, verbose_name='主图视频封面', related_name='Goods_video_cover', db_constraint=False)
    #商品服务，多对多
    services = ManyToManyField(GoodsService, null=True, blank=True,verbose_name='商品服务', related_name='Goods_services', db_constraint=False)
    #商品卖点
    selling_point = CharField(max_length=500, unique=False, null=True, blank=True,  verbose_name='商品卖点')
    #商品价格
    price = DecimalField(max_digits=10, decimal_places=2, unique=False, default=0, blank=True, verbose_name='商品价格')
    #商品详情
    detail = TextField(unique=False, null=True, blank=True,  verbose_name='商品详情')
    #商品库存
    stock = IntegerField(unique=False, default=0, blank=True, verbose_name='商品库存')
    #商品销量
    sales = IntegerField(unique=False, default=0, blank=True, verbose_name='商品销量')
    #商品初始销量
    initial_sales = IntegerField(unique=False, default=0, blank=True, verbose_name='商品初始销量')
    #商品实际销量
    actual_sales = IntegerField(unique=False, default=0, blank=True, verbose_name='商品实际销量')
    #商品状态1上架0下架
    status = IntegerField(unique=False, null=True,blank=True,default=1, verbose_name='商品状态')
    #商品排序
    sort = IntegerField(unique=False, null=True, default=1, blank=True, verbose_name='商品排序')#数字越小越靠前



